package handler

import (
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"goserver/internal/service"
)

type RobotCodeHandler struct {
	robotCodeService *service.RobotCodeService
}

func NewRobotCodeHandler(robotCodeService *service.RobotCodeService) *RobotCodeHandler {
	return &RobotCodeHandler{
		robotCodeService: robotCodeService,
	}
}

// CreateRobotCodes 创建机器码
func (h *RobotCodeHandler) CreateRobotCodes(c *gin.Context) {
	var req struct {
		Count      int `json:"count" binding:"required,min=1,max=100"`
		ExpireDays int `json:"expire_days" binding:"required,min=1,max=3650"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"error":   err.Error(),
		})
		return
	}

	// 从上下文获取用户ID
	userID := c.GetInt("user_id")
	if userID == 0 {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    401,
			"message": "未登录",
		})
		return
	}

	// 创建机器码
	codes, err := h.robotCodeService.CreateRobotCodes(c.Request.Context(), userID, req.Count, req.ExpireDays)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "创建失败",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "创建成功",
		"data":    codes,
	})
}

// GetRobotCodes 获取机器码列表
func (h *RobotCodeHandler) GetRobotCodes(c *gin.Context) {
	userID := c.GetInt("user_id")
	if userID == 0 {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    401,
			"message": "未登录",
		})
		return
	}

	codes, err := h.robotCodeService.GetUserRobotCodes(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "获取失败",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "获取成功",
		"data":    codes,
	})
}

// UpdateRobotCode 更新机器码状态
func (h *RobotCodeHandler) UpdateRobotCode(c *gin.Context) {
	var req struct {
		ID         int  `json:"id" binding:"required"`
		Status     int  `json:"status" binding:"required,oneof=0 1"`
		ExpireDays *int `json:"expire_days,omitempty" binding:"omitempty,min=1,max=3650"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"error":   err.Error(),
		})
		return
	}

	userID := c.GetInt("user_id")
	if userID == 0 {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    401,
			"message": "未登录",
		})
		return
	}

	// 获取机器码信息
	codes, err := h.robotCodeService.GetUserRobotCodes(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "更新失败",
			"error":   err.Error(),
		})
		return
	}

	// 验证机器码所有权
	var targetCode *service.RobotCode
	for _, code := range codes {
		if code.ID == req.ID {
			targetCode = code
			break
		}
	}

	if targetCode == nil {
		c.JSON(http.StatusForbidden, gin.H{
			"code":    403,
			"message": "无权操作此机器码",
		})
		return
	}

	// 更新状态
	targetCode.Status = req.Status
	if req.ExpireDays != nil {
		targetCode.ExpireTime = time.Now().AddDate(0, 0, *req.ExpireDays)
	}

	if err := h.robotCodeService.UpdateRobotCode(c.Request.Context(), targetCode); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "更新失败",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "��新成功",
		"data":    targetCode,
	})
}

// DeleteRobotCode 删除机器码
func (h *RobotCodeHandler) DeleteRobotCode(c *gin.Context) {
	codeID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"error":   err.Error(),
		})
		return
	}

	userID := c.GetInt("user_id")
	if userID == 0 {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    401,
			"message": "未登录",
		})
		return
	}

	if err := h.robotCodeService.DeleteRobotCode(c.Request.Context(), userID, codeID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "删除失败",
			"error":   err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "删除成功",
	})
} 